package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.GcFyhx;
import com.ruoyi.gcgl.mapper.GcFyhxMapper;
import com.ruoyi.gcgl.service.IGcFyhxService;

/**
 * 费用核销Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Service
public class GcFyhxServiceImpl implements IGcFyhxService 
{
    @Autowired
    private GcFyhxMapper gcFyhxMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询费用核销
     * 
     * @param id 费用核销主键
     * @return 费用核销
     */
    @Override
    public GcFyhx selectGcFyhxById(Long id)
    {
        return gcFyhxMapper.selectGcFyhxById(id);
    }

    /**
     * 查询费用核销列表
     * 
     * @param gcFyhx 费用核销
     * @return 费用核销
     */
    @Override
    public List<GcFyhx> selectGcFyhxList(GcFyhx gcFyhx)
    {
        return gcFyhxMapper.selectGcFyhxList(gcFyhx);
    }

    /**
     * 新增费用核销
     * 
     * @param gcFyhx 费用核销
     * @return 结果
     */
    @Override
    public int insertGcFyhx(GcFyhx gcFyhx)
    {
    	int rows = gcFyhxMapper.insertGcFyhx(gcFyhx);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcFyhx.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcFyhx.getOwner());
        variables.put("level1", gcFyhx.getZmr());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjbaoxiao", String.valueOf(gcFyhx.getId()), variables);
    	
        GcFyhx obj = new GcFyhx();
    	obj.setId(gcFyhx.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcFyhxMapper.updateGcFyhx(obj);
    	
        return rows;
    }

    /**
     * 修改费用核销
     * 
     * @param gcFyhx 费用核销
     * @return 结果
     */
    @Override
    public int updateGcFyhx(GcFyhx gcFyhx)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcFyhx.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcFyhx.getOwner());
//        variables.put("level1", gcFyhx.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjbaoxiao", String.valueOf(gcFyhx.getId()), variables);
//        gcFyhx.setProcessInstanceId(pi.getProcessInstanceId());
        return gcFyhxMapper.updateGcFyhx(gcFyhx);
    }

    /**
     * 批量删除费用核销
     * 
     * @param ids 需要删除的费用核销主键
     * @return 结果
     */
    @Override
    public int deleteGcFyhxByIds(String ids)
    {
        return gcFyhxMapper.deleteGcFyhxByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除费用核销信息
     * 
     * @param id 费用核销主键
     * @return 结果
     */
    @Override
    public int deleteGcFyhxById(Long id)
    {
        return gcFyhxMapper.deleteGcFyhxById(id);
    }

	@Override
	public int updateGcFyhx4check(GcFyhx gcFyhx) {
		// TODO Auto-generated method stub
		return gcFyhxMapper.updateGcFyhx(gcFyhx);
	}
}
